package com.atwl.school_system.service.serviceImpl;

import com.alibaba.druid.util.StringUtils;
import com.atwl.school_system.bean.ProInfo;
import com.atwl.school_system.bean.ProPic;
import com.atwl.school_system.bean.vo.ProInfoVo;
import com.atwl.school_system.mapping.ProDao;
import com.atwl.school_system.mapping.ProPicDao;
import com.atwl.school_system.service.ProService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProServiceImpl implements ProService {

    @Autowired
    private ProDao proDao;

    @Autowired
    private ProPicDao proPicDao;

    @Override
    public List<ProInfoVo> getProInfo(String id, String category, Integer sort) {
        String r_id = "", r_category = "";
        String r_sort = "ASC";
        if (!StringUtils.isEmpty(id)) r_id = id;
        if (!StringUtils.isEmpty(category)) r_category = category;
        if (sort == 0) r_sort = "ASC";
        else r_sort = "DESC";
        List<ProInfo> proInfos = proDao.getProInfo(r_id, r_category, r_sort);
        return getProInfoVos(proInfos);
    }

    public List<ProInfoVo> getProInfoVos(List<ProInfo> proInfos) {
        List<ProInfoVo> proInfoVos = new ArrayList<>();
        List<ProPic> proPics = new ArrayList<>();
        for (ProInfo proInfo : proInfos) {
            proPics = proPicDao.getProPics(Arrays.stream(proInfo.getProPicId().split(",")).map(Integer::parseInt).collect(Collectors.toList()));
            ProInfoVo proInfoVo = new ProInfoVo();
            proInfoVo.setProUrl(proPics.stream().map(ProPic::getPicUrl).collect(Collectors.toList()));
            BeanUtils.copyProperties(proInfo, proInfoVo);
            proInfoVos.add(proInfoVo);
        }
        return proInfoVos;
    }

    @Override
    public List<ProInfoVo> getProInfoAll() {
        List<ProInfo> proInfos = proDao.getProInfoAll();
        return getProInfoVos(proInfos);
    }

    @Override
    public void upPro(ProInfoVo proInfoVo) {
        StringBuilder sb = new StringBuilder();
        for (String url : proInfoVo.getProUrl()) {
            Integer id = proPicDao.upProPic(url);
            sb.append(id).append(",");
        }
        sb.substring(0, sb.length() - 1);
        proDao.upPro(proInfoVo.getProName(), proInfoVo.getProPrice(), proInfoVo.getProCategory(), sb.toString(), new Date());
    }

    @Override
    public List<ProInfo> searchForName(String keyWords, Integer num) {
        if (!StringUtils.isEmpty(keyWords)) {
            return proDao.searchForName(keyWords, num);
        } else {
            return null;
        }
    }

    @Override
    public void deleteProduct(String pid) {
        proDao.deleteProduct(pid);
    }

    @Override
    public List<ProInfoVo> getProHot(Integer lim) {
        List<ProInfo> proHot = proDao.getProHot(lim);
        return getProInfoVos(proHot);
    }
}
